import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

class VideoPage extends StatefulWidget {
  final String? url;
  VideoPage({Key? key, this.url}) : super(key: key);
  @override
  _VideoPageState createState() => _VideoPageState();
}

class _VideoPageState extends State<VideoPage> {
  VideoPlayerController? _controller;

  @override
  void initState() {
    if(!(widget.url?.contains('https')??false)){
      widget.url?.replaceFirst('http', 'https');
    }
    _controller =
    VideoPlayerController.networkUrl(Uri.parse(widget.url??''))
      ..initialize().then((_) {
        setState(() {
          _controller?.play();
        });
      });

    // Use the controller to loop the video.
    _controller?.setLooping(true);

    super.initState();
  }

  @override
  void dispose() {
    // Ensure disposing of the VideoPlayerController to free up resources.
    _controller?.dispose();

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(''),
      ),
      body: Center(
        child: (_controller?.value.isInitialized??false)
            ? AspectRatio(
          aspectRatio: _controller?.value.aspectRatio??0,
          child: VideoPlayer(_controller!),
        )
            : CircularProgressIndicator(),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            if (_controller?.value.isPlaying??false) {
              _controller?.pause();
            } else {
              _controller?.play();
            }
          });
        },
        child: Icon(
          (_controller?.value.isPlaying??false) ? Icons.pause : Icons.play_arrow,
        ),
      ),
    );
  }
}